<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/20
 * Time: 06:07
 */

namespace HiCommon\Repository;

use DB;
use HiCommon\Model\RepairstoreGoods;
use HiCommon\Model\Shop;

class RepairstoreGoodsRepository extends BaseRepository
{
    public function base_model(){
        return new \HiCommon\Model\RepairstoreGoods();
    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($arr_option = [])
    {
        return RepairstoreGoods::commonget($arr_option);
    }


    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($option = [])
    {
        $model = new RepairstoreGoods();

        $model = $model->where('repairstore_id', $option['repairstore_id']);

        if (isset($option['name']) && $option['name']) {
            $model = $model->where('name', 'like', '%' . $option['name'] . '%');
        }

        if (isset($option['type']) && $option['type']) {
            $model = $model->where('type', $option['type']);
        }

        return $model->with(['type'])->commonpage();
    }


    //api接口
    public function small_get_list($option = [])
    {
        $longitude = $option['lon'];
        $latitude = $option['lat'];

        $field_distance = DB::raw("( 2 * 6378.137 * asin(sqrt(pow(sin((pi()/180*($latitude) - pi()/180*(latitude)) / 2), 2) + cos(pi()/180*($latitude)) * cos(pi()/180*(latitude)) * pow(sin((pi()/180*($longitude) - pi()/180*(longitude)) / 2), 2)))) as distance");
        //$field_distance = DB::raw("(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*($longitude-longitude)/360),2)+COS(PI()*$latitude/180)* COS(latitude * PI()/180)*POW(SIN(PI()*($latitude-latitude)/360),2)))) as distance");
        $arr_select = [
            'id', 'repairstore_id', 'name', 'type', 'price', $field_distance,
        ];

        $model = new RepairstoreGoods();

        if (isset($option['type']) && $option['type']) {
            $model = $model->where('type', $option['type']);
        }

        if (isset($option['distance']) && $option['distance']) {
            $model = $model->where(DB::raw("( 2 * 6378.137 * asin(sqrt(pow(sin((pi()/180*($latitude) - pi()/180*(latitude)) / 2), 2) + cos(pi()/180*($latitude)) * cos(pi()/180*(latitude)) * pow(sin((pi()/180*($longitude) - pi()/180*(longitude)) / 2), 2))))"), '<=', $option['distance']);
        }
        $result = $model
            ->select($arr_select)
            ->with(['type', 'pic', 'shop' => function ($query) {
                $query->with([/*'province_name', */ 'city_name', 'area_name']);
            }])
            ->where(
                function ($query) {
                    $query->whereHas('shop',
                        function ($query) {
                            $query->where(['status' => 1, 'state' => 1]);
                        });
                }
            )
            ->where(
                function ($query)
                use ($option) {
                    $option['province'] = intval($option['province']);
                    if ($option['province'] > 0) {
                        $query->whereHas('shop',
                            function ($query)
                            use ($option) {
                                $query->where('province', $option['province']);
                            });
                    }

                    $option['city'] = intval($option['city']);
                    if ($option['city'] > 0) {
                        $query->whereHas('shop',
                            function ($query)
                            use ($option) {
                                $query->where('city', $option['city']);
                            });
                    }

                    $option['area'] = intval($option['area']);
                    if ($option['area'] > 0) {
                        $query->whereHas('shop',
                            function ($query)
                            use ($option) {
                                $query->where('area', $option['area']);
                            });
                    }
                }
            )
            ->where('status', 1)
            ->orderBy('distance', 'asc')
            ->paginate();

        return $result;
    }


    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return RepairstoreGoods::where($arr_where)->count();
    }

    /**
     * 根据条件统计总和
     *
     * @param array $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [], $s_field = 'mark')
    {
        return RepairstoreGoods::where($arr_where)->sum($s_field);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        return RepairstoreGoods::create($arr_input);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return RepairstoreGoods::with(['pic'])->commonfind($n_id, $arr_option);
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return RepairstoreGoods::where('id', $arr_input['id'])->update($arr_input);
    }

    //根据条件修改数据
    public function edit_one_by_where(array $map, array $option)
    {
        return RepairstoreGoods::where($map)->update($option);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return RepairstoreGoods::where('id', $n_id)->commondelete();
    }

    /**
     * 删除多条数据
     */
    public function delete_more(array $id_array)
    {
        return RepairstoreGoods::whereIn('id', $id_array)->commondelete();
    }

    /**
     * 修改一条数据状态
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id, int $n_state)
    {
        return RepairstoreGoods::where('id', $n_id)->update(['status' => $n_state]);
    }

    /**
     * 字段自增
     *
     * @param int $n_id
     * @param string $s_field
     * @param int $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id, $s_field = 'commend_num', $n_step = 1)
    {
        return RepairstoreGoods::where('id', $n_id)->increment($s_field, $n_step);
    }

    /**
     * 字段自减
     *
     * @param int $n_id
     * @param string $s_field
     * @param int $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id, $s_field = 'commend_num', $n_step = 1)
    {
        return RepairstoreGoods::where('id', $n_id)->decrement($s_field, $n_step);
    }

    /**
     * 获取商品价格
     * @param int $n_id
     * @param array $arr_option
     * @return mixed
     */
    public function get_goods_price(int $n_id)
    {
        return RepairstoreGoods::where('id', $n_id)->first(['price', 'name', 'type', 'repairstore_id']);
    }


    /*************************************************接口******************************************************/

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_get_list($option = [])
    {
        $model = new RepairstoreGoods();

        if (isset($option['shop_id']) && $option['shop_id']) {
            $model = $model->where('repairstore_id', $option['shop_id']);
        }
        if (isset($option['status']) && $option['status']) {
            $model = $model->where('status', $option['status']);
        }

        return $model->with(['shop', 'type', 'pic'])
            ->select('id', 'repairstore_id', 'name', 'type', 'price', 'status')
            ->orderBy('id','desc')
            ->paginate();
    }


    /**
     * 用户API接口用
     */
    public function by_where_limit(array $option)
    {
        return RepairstoreGoods::where($option['where'])
            ->with($option['with'])
            ->orderBy('updated_at', 'desc')
            ->limit($option['limit'])
            ->get($option['field'])
            ->toArray();
    }
}